package net.zoneland.knowledge.utils;

import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

/**
 * 日期辅助类.
 *
 * @author hh
 * Jul 10, 2010 12:34:20 AM
 */
public final class DateHelper {
    /**
     * 默认日期格式字符串.
     */
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
    public static final String DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static final String DATE_PATTERN_WITHOUT_SECOND = "yyyy-MM-dd HH:mm";

    /**
     * 中文日期格式。
     */
    public static final String CHINA_DATE_FORMAT_STR = "yyyy年MM月dd日";

    /**
     * 日期格式化
     */
    public static final DateTimeFormatter CHINA_DATE_FORMATTER = DateTimeFormatter.ofPattern(DATE_PATTERN);

    /**
     * 私有构造.
     */
    private DateHelper() {

    }

    /**
     * 获取日期字符串.
     *
     * @param aMask 日期格式
     * @param aDate 日期
     * @return 字符串
     */
    public static final String getDateTime(final String aMask, final Date aDate) {
        return Optional.ofNullable(aDate).map(it -> DateFormatUtils.format(it, aMask)).orElse("");
    }

    /**
     * 获取日期字符串.
     *
     * @param aDate 日期
     * @return 字符串
     */
    public static final String getDateTime(final Date aDate) {
        return Optional.ofNullable(aDate).map(it -> DateFormatUtils.format(it, "yyyy-MM-dd HH:mm:ss")).orElse("");
    }
    /**
     * 获取日期字符串.
     *
     * @param aDate 日期
     * @return 字符串
     */
    public static final String getLocalDateTime(final LocalDateTime aDate) {
        return Optional.ofNullable(aDate).map(it -> it.format(CHINA_DATE_FORMATTER)).orElse("");
    }
    /**
     * Date 转 String.
     *
     * @param date 日期
     * @return 字符串
     */
    public static String getStringDate(final Date date) {
        return Optional.ofNullable(date).map(it -> DateFormatUtils.format(it, DateHelper.DEFAULT_DATE_FORMAT)).orElse("");
    }

    /**
     * @param startTime 开始日期
     * @param endTime   结束日期
     * @return 日期集合
     * @throws ParseException 异常
     * @description: 获取两个日期之间的所有日期（参数及返回日期均为字符串格式）.
     */
    public static List<String> getTimesBetweenDates(final String startTime, final String endTime) throws ParseException {
        Date start = DateUtils.parseDate(startTime, DateHelper.DEFAULT_DATE_FORMAT);
        Date end = DateUtils.parseDate(endTime, DateHelper.DEFAULT_DATE_FORMAT);
        // 如果时间传反了
        final long time1 = start.getTime();
        final long time2 = end.getTime();
        Date midTime = new Date();
        if (time1 > time2) {
            midTime = start;
            start = end;
            end = midTime;
        }
        final List<String> result = new ArrayList<String>();
        final Calendar startCal = Calendar.getInstance();
        startCal.setTime(start);

        final Calendar endCal = Calendar.getInstance();
        endCal.setTime(end);
        // 日期+1,将结束日期放入结果集
        endCal.add(Calendar.DATE, +1);
        while (startCal.before(endCal)) {
            result.add(DateHelper.getStringDate(startCal.getTime()));
            startCal.add(Calendar.DAY_OF_YEAR, 1);
        }
        return result;
    }

}
